package com.gqz.qrcode.controller;

import cn.hutool.crypto.SmUtil;
import com.gqz.common.core.domain.R;
import com.gqz.qrcode.util.QRCodeUtil;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;


/**
 * qrcode控制器
 *
 * @author gqz20
 * @date 2023/02/11
 */
@RestController
@RequestMapping("/qrcode")
@Slf4j
public class QRCodeController {
    //key必须是16字节，即128位
    private String key = "SGemsBJ-1234!@#$";
    private String prefix = "SM4(";
    private String subfix = ")";

    @GetMapping("/getQRCodeBase64")
    public R getQRCodeBase64(@RequestParam("content") String content) {
        var encryptContent = SmUtil.sm4(key.getBytes()).encryptHex(content);
        var title = content.split(";");
        var no = "";
        if (title.length > 0) {
            for (int i = 0; i < title.length; i++) {
                if (title[i].contains("出厂编码")) {
                    no = title[i].split("=")[1];
                    break;
                }
            }
        }
        return R.ok(QRCodeUtil.getBase64QRCode(encryptContent, no));
    }

    @GetMapping("/getQRCode")
    public void getQRCode(HttpServletResponse response, @RequestParam("content") String content) throws IOException {
        //生成二维码规则 设备编码=1585954186420109314;出厂编码=HS-001-SNDJ-005;设备类型=100001;设备名称=设备名称;
        ServletOutputStream stream = null;
        try {
            stream = response.getOutputStream();
            String encryptContent = prefix + SmUtil.sm4(key.getBytes()).encryptHex(content) + subfix;
            String[] title = content.split(";");
            String no = "";
            if (title.length > 0) {
                for (int i = 0; i < title.length; i++) {
                    if (title[i].contains("出厂编码")) {
                        no = title[i].split("=")[1];
                        break;
                    }
                }
            }
            QRCodeUtil.getQRCode(encryptContent, stream, no);
            log.info("明文：{}    密文：{}", content, encryptContent);
            log.info("明文：{} ", SmUtil.sm4(key.getBytes()).decryptStr(encryptContent.substring(prefix.length(), encryptContent.length() - 1)));
        } catch (Exception e) {
            e.getStackTrace();
        } finally {
            if (stream != null) {
                stream.flush();
                stream.close();
            }
        }
    }

    @PostMapping("/decryptQRCode")
    public R decryptQRCode(@RequestBody String content) {
        String decryptContent = "";
        if (content.startsWith(prefix) && content.endsWith(subfix)) {
            String encryptContent = content.substring(prefix.length(), content.length() - 1);
            log.info("接收数据：{}", content);
            log.info("过滤后数据：{}", encryptContent);
            decryptContent = SmUtil.sm4(key.getBytes()).decryptStr(encryptContent);
        }
        log.info("解密后：{}", decryptContent);
        //去获取设备信息
        return R.ok(decryptContent);
    }
}
